<template>
	<!-- #ifdef APP-NVUE -->
	<cell>
		<!-- #endif -->
		<view :class="disabled ? 'uni-list-item--disabled' : ''" :hover-class="disabled || showSwitch ? '' : 'uni-list-item--hover'"
		 class="uni-list-item" @click="onClick">
			<view class="uni-list-item__container" :class="{'uni-list-item--first':isFirstChild}">
				<view v-if="thumb" class="uni-list-item__icon">
					<image :src="thumb" class="uni-list-item__icon-img" />
				</view>
				<view v-else-if="showExtraIcon" class="uni-list-item__icon">
					<uni-icons :color="extraIcon.color" :size="extraIcon.size" :type="extraIcon.type" class="uni-icon-wrapper" />
				</view>
				<view v-if="!customedContent" class="uni-list-item__content">
					<slot />
					<view class="uni-list-itme-content-hd">
                        <view :class="['uni-list-item__content-title', 
                                      required ? 'uni-list-item__content-title-required' : '']">
                            {{ title }}
                        </view>
                        <view v-if="rank" class="uni-list-item__content-rank" @click="clickRank">
                            {{ rank }}
                        </view>
                    </view>
					<text v-if="note" class="uni-list-item__content-note">{{ note }}</text>
				</view>
				<view v-if="showBadge || showArrow || showSwitch ||showEndIcon || showRadio || showPlainText || showButton" class="uni-list-item__extra">
					<text v-if="rightText" class="uni-list-item__extra-text">{{rightText}}</text>
					<uni-badge v-if="showBadge" :type="badgeType" :text="badgeText" />
					<switch v-if="showSwitch" :disabled="disabled" :checked="switchChecked" @change="onSwitchChange" />
					<radio v-if="showRadio" :disabled="disabled" :checked="radioChecked" color="rgba(102,134,255,1)"></radio>
                    <view v-if="showPlainText" class="uni-list-item__extra-plain" :style="plainStyle">{{plainText}}</view>
					<uni-icons v-if="showArrow" :size="20" class="uni-icon-wrapper" color="rgba(102,134,255,1)" type="arrowright" />
					<image v-if="showEndIcon" class="uni-icon-endcircle" :src="thumbend" />
                    <button v-if="showButton" 
                            :class="[buttonDisabled ? 'btn-disabled' : '', 'mini-btn']" 
                            type="primary" 
                            size="mini"
                            @click="BtnClick(buttonDisabled)">
                        {{buttonContent}}
                    </button>
				</view>
                <slot name="customedContentSlot"></slot>
			</view>

		</view>
		<!-- #ifdef APP-NVUE -->
	</cell>
	<!-- #endif -->
</template>

<script>
	import uniIcons from '../uni-icons/uni-icons.vue'
	import uniBadge from '../uni-badge/uni-badge.vue'

	/**
	 * ListItem 列表子组件
	 * @description 列表子组件
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=24
	 * @property {String} title 标题
	 * @property {String} note 描述
	 * @property {String} thumb 左侧缩略图，若thumb有值，则不会显示扩展图标
	 * @property {String} badgeText 数字角标内容
	 * @property {String} badgeType 数字角标类型，参考[uni-icons](https://ext.dcloud.net.cn/plugin?id=21)
	 * @property {String} rightText 右侧文字内容
	 * @property {Boolean} disabled = [true|false]是否禁用
	 * @property {Boolean} showArrow = [true|false] 是否显示箭头图标
	 * * @property {Boolean} showEndIcon = [true|false] 是否显示箭头图片
	 * @property {Boolean} showBadge = [true|false] 是否显示数字角标
	 * @property {Boolean} showSwitch = [true|false] 是否显示Switch
	 * @property {Boolean} switchChecked = [true|false] Switch是否被选中
	 * @property {Boolean} showExtraIcon = [true|false] 左侧是否显示扩展图标
	 * @property {Boolean} scrollY = [true|false] 允许纵向滚动，需要显式的设置其宽高
	 * @property {Object} extraIcon 扩展图标参数，格式为 {color: '#4cd964',size: '22',type: 'spinner'}
	 * @event {Function} click 点击 uniListItem 触发事件
	 * @event {Function} switchChange 点击切换 Switch 时触发
	 */
	export default {
		name: 'UniListItem',
		components: {
			uniIcons,
			uniBadge
		},
		props: {
			title: {
				type: String,
				default: ''
			}, // 列表标题
            required: { // 是否在标题后面追加必填*
                type: Boolean,
                default: false
            },
            rank: { // 列表标签/(头衔)
                type: String,
                default: ''
            },
            buttonContent: { // 按钮内容
                type: String,
                default: "按钮"
            },
            buttonDisabled: { // 按钮是否可用
                type: Boolean,
                default: false
            },
			note: {
				type: String,
				default: ''
			}, // 列表描述
			disabled: {
				// 是否禁用
				type: [Boolean, String],
				default: false
			},
			showArrow: {
				// 是否显示箭头
				type: [Boolean, String],
				default: false
			},
			showEndIcon: {
				// 是否显示箭头
				type: [Boolean, String],
				default: false
			},
			showBadge: {
				// 是否显示数字角标
				type: [Boolean, String],
				default: false
			},
			showSwitch: {
				// 是否显示Switch
				type: [Boolean, String],
				default: false
			},
			showRadio: {
				// 是否显示Radio
				type: [Boolean, String],
				default: false
			},
            showButton: { // 是否显示按钮
                type: Boolean,
                default: false
            },
            showPlainText: { // 是否显示纯文本
                type: Boolean,
                default: false
            },
            plainText: { // 链接右边显示的纯文本内容
                type: String,
                default: ''
            },
            plainStyle: { // 链接右边显示的纯文字的样式
                type: String,
                default: ''
            },
			switchChecked: {
				// Switch是否被选中
				type: [Boolean, String],
				default: false
			},
			radioChecked: {
				// Radio是否被选中
				type: [Boolean, String],
				default: false
			},
			badgeText: {
				// badge内容
				type: String,
				default: ''
			},
			badgeType: {
				// badge类型
				type: String,
				default: 'success'
			},
			rightText: {
				// 右侧文字内容
				type: String,
				default: ''
			},
			thumb: {
				// 缩略图
				type: String,
				default: ''
			},
			thumbend: {
				type: String,
				default: ''
			},
			showExtraIcon: {
				// 是否显示扩展图标
				type: [Boolean, String],
				default: false
			},
			extraIcon: {
				type: Object,
				default () {
					return {
						type: 'contact',
						color: '#000000',
						size: 20
					}
				}
			},
            customedContent: {  // 是否自定义uni-list-item__content中内容
                type: Boolean,
                default: false
            }
		},
		inject: ['list'],
		data() {
			return {
				isFirstChild: false,
			}
		},
		mounted() {
			if (!this.list.firstChildAppend) {
				this.list.firstChildAppend = true
				this.isFirstChild = true
			}
		},
		methods: {
			onClick() {
				this.$emit('click')
			},
			onSwitchChange(e) {
				this.$emit('switchChange', e.detail)
			},
            BtnClick: function(buttonDisabled) {
                console.log("点击按钮" + buttonDisabled);
                if (!buttonDisabled) {
                    this.$emit('btn-click');
                }
            },
		}
	}
</script>


<style lang="scss" scoped>
	@charset "UTF-8";


	.uni-list-item {

		font-size: 32rpx;

		font-size: $uni-font-size-base;

		position: relative;
		flex-direction: column;
		justify-content: space-between;
		padding-left: 30rpx;
	}

	.uni-list-item--disabled {
		opacity: 0.3;
	}

	.uni-list-item--hover {
		background-color: #f1f1f1;
	}

	.uni-list-item__container {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		padding-bottom: 7rpx;
		padding-left: 0;
		flex: 1;
		position: relative;
		justify-content: space-between;
		align-items: center;
		/* #ifdef APP-PLUS */
		border-top-color: #e5e5e5;
		border-top-style: solid;
		border-top-width: 0.5px;
		/* #endif */
	}

	.uni-list-item--first {
		border-top-width: 0px;
	}

	/* #ifndef APP-NVUE */
	.uni-list-item__container:after {
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);

		background-color: #e5e5e5;

	}

	.uni-list-item--first:after {
		height: 0px;
	}

	/* #endif */


	.uni-list-item__content-rank,
	.uni-list-item__content-title {
		font-size: 22rpx;
		text-overflow: ellipsis;
		white-space: nowrap;
		color: #323638;
		overflow: hidden
	}


	.uni-list-item__content-rank {
		font-size: $uni-font-size-sm;
		color: #999;
		margin-left: 16upx;
		border: 1upx solid #999;
		border-radius: 26upx;
		padding-left: 14upx;
		padding-right: 14upx;
		margin: auto 0 auto 16upx;
		line-height: 36upx;
	}
    
    .uni-list-itme-content-hd {
        flex: 1;
        display: flex;
        overflow: hidden;
        flex-direction: row;
        justify-content: flex-start;
    }


	.uni-list-item__content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		overflow: hidden;
		flex-direction: column;
		color: #3b4144;

	}

	.uni-list-item__content-title {
		font-size: 22rpx;
		color: #323638;
		overflow: hidden;
	}

	.uni-list-item__content-note {
		margin-top: 6rpx;
		color: #999;
		font-size: $uni-font-size-sm;
		white-space: normal;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden
	}

	.uni-list-item__extra {
		/* width: 25%;
 */
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: flex-end;
		align-items: center;
	}

	.uni-list-item__icon {
		margin-right: 18rpx;
        display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 38rpx;
	}

	.uni-list-item__icon-img {
		height: 52rpx;
		width: 52rpx;
	}

	.uni-icon-endcircle {
		height: 32rpx;
		width: 32rpx;
		border-radius: $uni-border-radius-circle;
		margin-left: 8rpx;
	}

	.uni-list-item__extra-text {
		color: #999;
		font-size: 24rpx;
		line-height: 26rpx;
	}
    
    .uni-list-item__extra-plain {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: flex-end;
    }
    
    button.btn-disabled {
        background-color: #9ED99D !important;
        color: rgba(255, 255, 255, 0.6) !important;
    }
</style>
